<style lang="less">
  .standard-price-version{
    .el-upload-list {
      display: inline-block;
    }
    .el-dialog__body {
      padding: 10px;
    }
    .el-tabs__header{
      margin: 0px;
    }
    .line,.price {
      border: 1px solid #dfe6ec;
    }
    .line{
      margin-right: 10px;
    }
    .el-tabs__content{
      padding: 10px;
    }
    .version {
      border: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      .header {
        border-bottom: 1px solid #dfe6ec;
        padding: 0 15px;
        height: 43px;
        line-height: 43px;
        background-color: #fff;
        color: #48576a;
        font-size: 13px;
      }
      .header-body{
        padding: 0 15px;
        .el-row{
          .el-col{
            div {
              height: 43px;
              line-height: 43px;
            }
          }
          .el-col-3{
            margin-top: 43px;
            text-align: right;
          }
        }
      }
    }
  }
</style>

<template>
  <el-dialog title="维护运价" :modal="false" :visible.sync="dialogVisible"
    class="standard-price-version"
    fullscreen>
    <div class="version">
      <div class="header">
        <span>运价编号：{{standardPrice.number}}</span><span style="float: right">[{{standardPrice.createUserName}}]{{standardPrice.createDate}}</span>
      </div>
      <div class="header-body">
        <el-row>
          <el-col :span="7">
            <div>运价名称：{{standardPrice.name}}</div>
            <div>目的地组织：{{standardPrice.organizationNames}}</div>
          </el-col>
          <el-col :span="7">
            <div>发布渠道：{{standardPrice.stowageName}}</div>
            <div>备注：{{standardPrice.description}}</div>
          </el-col>
          <el-col :span="7">
            <div>运价类型：{{standardPrice.typeName}}</div>
            <div></div>
          </el-col>
          <el-col :span="3">
            <el-button type="primary" @click="addStandardPriceVersionClick">新增</el-button>
          </el-col>
        </el-row>
        <el-table
          :data="versionData"
          border
          highlight-current-row
          v-loading="listLoading"
          @current-change="handleStandardPriceVersionCurrentChange"
          height="250"
          style="width: 100%">
          <el-table-column
            type="index"
            width="50">
          </el-table-column>
          <el-table-column
            prop="number"
            label="版本号"
            width="200">
          </el-table-column>
          <el-table-column
            width="200"
            prop="statusName"
            label="提交状态">
          </el-table-column>
          <el-table-column
            width="200"
            prop="submitTime"
            label="提交时间">
          </el-table-column>
          <el-table-column
            prop="description"
            label="备注">
          </el-table-column>
          <el-table-column
            width="280"
            label="操作">
            <template slot-scope="scope">
              <el-button type="primary" v-if="scope.row.status ===112014002" @click="copyStandardPriceVersionClick(scope.row.id)">复制新增</el-button>
              <el-button type="primary" v-if="scope.row.status !==112014002" @click="submitStandardPriceVersionClick(scope.row.id)">提交</el-button>
              <el-button type="danger" v-if="scope.row.status !==112014002" @click="deleteStandardPriceVersionClick(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-row style="margin: 0">
      <el-col :span="12">
        <div style="margin-right: 10px;padding: 10px;border: 1px solid #dfe6ec;height: 365px;">
          <el-row>
            <el-col :span="8">
              <label>线路名称：</label>
              <el-input style="width: auto;" v-model="standardPriceVersionLine.searchCondition.LIKE_name"  placeholder="线路名称"></el-input>
            </el-col>
            <el-col :span="8">
              <label>运输方式：</label>
              <el-select v-model="standardPriceVersionLine.searchCondition.EQ_ageingType"  placeholder="运输方式">
                <el-option style="width: 200px"
                           v-for="item in $store.getters.AGING_CLASSIFICATION"
                           :label="item.cnDictValname"
                           :key = "item.id"
                           :value="item.dictVal"></el-option>
              </el-select>
            </el-col>
            <el-col :span="8">
              <label>运价类型：</label>
              <el-select v-model="standardPriceVersionLine.searchCondition.EQ_lineType"  placeholder="运价类型">
                <el-option style="width: 200px"
                           v-for="item in $store.getters.LINE_TYPE"
                           :label="item.cnDictValname"
                           :key = "item.id"
                           :value="item.dictVal"></el-option>
              </el-select>
            </el-col>
          </el-row>
          <el-row style="margin: 10px 0;">
            <el-button type="primary" :disabled="selectedStandardPriceVersion === null || selectedStandardPriceVersion.status === 112014002" @click="addStandardPriceVersionLineClick">新增</el-button>
            <el-button type="primary" :disabled="selectedStandardPriceVersion === null || selectedStandardPriceVersion.status === 112014002" @click="clearStandardPriceVersionLineClick">清空</el-button>
            <el-button type="primary" :disabled="selectedStandardPriceVersion === null" @click="exportStandardPriceVersionLines">导出</el-button>
            <el-upload
              class="upload-demo"
              style="display: inline-block"
              :file-list="fileList"
              :on-success="handleOnUploadSuccess"
              :action="'/api/v2.0/admin/standard-price-versions/'+(selectedStandardPriceVersion === null ? '0': selectedStandardPriceVersion.id)+'/upload' ">
              <el-button  type="primary" :disabled="selectedStandardPriceVersion === null || selectedStandardPriceVersion.status === 112014002">导入</el-button>
            </el-upload>
            <el-button style="float: right;margin-left: 10px;" @click="searchStandardPriceVersionLines" type="primary" :disabled="selectedStandardPriceVersion === null">查询</el-button>
            <el-button style="float: right;margin-left: 10px;" @click="resetStandardPriceVersionLineCondition" type="primary" :disabled="selectedStandardPriceVersion === null">重置</el-button>
          </el-row>
          <el-table
            :data="standardPriceVersionLine.lineData"
            height="250"
            border
            highlight-current-row
            v-loading="standardPriceVersionLine.listLoading"
            @current-change="handleStandardPriceVersionLineCurrentChange"
            style="width: 100%">
            <el-table-column
              label="线路名称"
              prop="name"
              width="180">
            </el-table-column>
            <el-table-column
              prop="lineTypeName"
              width="100"
              label="运价类型">
            </el-table-column>
            <el-table-column
              width="100"
              prop="ageingTypeName"
              label="运输方式">
            </el-table-column>
            <el-table-column
              width="100"
              prop="ageingScope"
              label="时效范围">
            </el-table-column>
            <el-table-column
              width="100"
              prop="startingPrice"
              label="起步收费">
            </el-table-column>
            <el-table-column
              width="100"
              prop="description"
              label="备注">
            </el-table-column>
            <el-table-column
              min-width="160"
              label="操作">
              <template slot-scope="scope">
                <el-button type="primary" @click="modifyStandardPriceVersionLineClick(scope.row)" :disabled="selectedStandardPriceVersion === null || selectedStandardPriceVersion.status === 112014002">修改</el-button>
                <el-button type="danger" @click="deleteStandardPriceVersionLineClick(scope.row.id)" :disabled="selectedStandardPriceVersion === null || selectedStandardPriceVersion.status === 112014002">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-col :span="24" class="toolbar">
            <el-pagination
              :current-page="standardPriceVersionLine.page"
              :page-size="standardPriceVersionLine.size"
              @size-change="handleSizeChange"
              @current-change="handleCurrentPageChange"
              layout="total,sizes,prev, pager, next, jumper"
              :total="standardPriceVersionLine.total" style="float: right">
            </el-pagination>
          </el-col>
        </div>
      </el-col>
      <el-col :span="12" class="price">
        <el-tabs @tab-click="tabClick" active-name="weightTab">
          <el-tab-pane label="重货" name="weightTab">
            <standard-price-version-price unit="吨" :billing-type="112003001" ref="weightPrice" :btn-disabled="selectedStandardPriceVersion === null || selectedStandardPriceVersion.status === 112014002"></standard-price-version-price>
          </el-tab-pane>
          <el-tab-pane label="抛货" name="volumeTab">
            <standard-price-version-price unit="方" :billing-type="112003002" ref="volumePrice" :btn-disabled="selectedStandardPriceVersion === null || selectedStandardPriceVersion.status === 112014002"></standard-price-version-price>
          </el-tab-pane>
          <el-tab-pane label="按件数" name="amountTab">
            <standard-price-version-price unit="件" :billing-type="112003003" ref="amountPrice" :btn-disabled="selectedStandardPriceVersion === null || selectedStandardPriceVersion.status === 112014002"></standard-price-version-price>
          </el-tab-pane>
        </el-tabs>
      </el-col>
    </el-row>
    <standard-price-version-add ref="standardPriceVersionAdd" @comfirmSuccess="saveStandardPriceVersion"></standard-price-version-add>
    <standard-price-version-line-add ref="standardPriceVersionLineAdd" @comfirmSuccess="saveStandardPriceVersionLine"></standard-price-version-line-add>
  </el-dialog>
</template>

<script>
import {
  getAllStandardPriceVersions,
  createStandardPriceVersion,
  emptyStandardPriceVersion,
  submitStandardPriceVersion,
  copyStandardPriceVersion,
  deleteStandardPriceVersion} from '../../../api/freight/StandardPriceVersion';
import {
  getAllStandardPriceVersionLines,
  createStandardPriceVersionLine,
  deleteStandardPriceVersionLine
} from '../../../api/freight/StandardPriceVersionLine';
import StandardPriceVersionPrice from './StandardPriceVersionPrice.vue';
import StandardPriceVersionAdd from './StandardPriceVersionAdd.vue';
import StandardPriceVersionLineAdd from './StandardPriceVersionLineAdd.vue';
export default {
  components: {
    StandardPriceVersionLineAdd,
    StandardPriceVersionAdd,
    StandardPriceVersionPrice},
  name: 'StandardPriceVersion',
  componentName: 'StandardPriceVersion',
  data () {
    return {
      dialogVisible: false,
      listLoading: false,
      versionData: [],
      standardPrice: {},
      selectedStandardPriceVersion: null,
      standardPriceVersionLine: {
        searchCondition: {
          LIKE_name: null,
          EQ_ageingType: null,
          EQ_lineType: null
        },
        listLoading: false,
        lineData: [],
        page: 1,
        size: 10,
        total: 0
      },
      fileList: []
    };
  },
  methods: {
    handleSizeChange (val) {
      this.standardPriceVersionLine.size = val;
      this.searchStandardPriceVersionLines();
    },
    handleCurrentPageChange (val) {
      this.standardPriceVersionLine.page = val;
      this.searchStandardPriceVersionLines();
    },
    copyStandardPriceVersionClick (id) {
      this.$prompt('您确认要复制新增该版本?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPlaceholder: '请输入备注'
      }).then(({ value }) => {
        copyStandardPriceVersion(id, value)
          .then(res => {
            if (res.data.successFlg) {
              this.$message('复制成功！');
              this.loadVersions();
            } else {
              this.$message.error(res.data.errorMsg);
            }
          });
      }).catch(() => {});
    },
    handleOnUploadSuccess (response) {
      this.fileList = [];
      if (response.successFlg) {
        this.searchStandardPriceVersionLines();
        this.$message('导入成功');
      } else {
        this.$message.error(response.errorMsg);
      }
    },
    exportStandardPriceVersionLines () {
      location.href = '/api/v2.0/admin/standard-price-versions/' + this.selectedStandardPriceVersion.id + '/export';
    },
    clearStandardPriceVersionLineClick () {
      this.$confirm('您确认清空所有线路及其价格?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        emptyStandardPriceVersion(this.selectedStandardPriceVersion.id)
          .then(res => {
            if (res.data.successFlg) {
              this.$message('清空成功！');
              this.searchStandardPriceVersionLines();
            } else {
              this.$message.error(res.data.errorMsg);
            }
          });
      }).catch(() => {});
    },
    tabClick (v) {
      if (v.name === 'weightTab') {
        this.$refs.weightPrice.load();
      } else if (v.name === 'volumeTab') {
        this.$refs.volumePrice.load();
      } else {
        this.$refs.amountPrice.load();
      }
    },
    handleStandardPriceVersionLineCurrentChange (val) {
      if (val) {
        this.$refs.weightPrice.show(val.id);
        this.$refs.volumePrice.show(val.id);
        this.$refs.amountPrice.show(val.id);
      } else {
        this.$refs.weightPrice.clear();
        this.$refs.volumePrice.clear();
        this.$refs.amountPrice.clear();
      }
    },
    modifyStandardPriceVersionLineClick (o) {
      this.$refs.standardPriceVersionLineAdd.show(o);
    },
    addStandardPriceVersionLineClick () {
      this.$refs.standardPriceVersionLineAdd.show();
    },
    resetStandardPriceVersionLineCondition () {
      this.$ue.resetObjNull(this.standardPriceVersionLine.searchCondition);
    },
    searchStandardPriceVersionLines () {
      if (this.selectedStandardPriceVersion) {
        this.standardPriceVersionLine.listLoading = true;
        getAllStandardPriceVersionLines({
          filters: this.$ue.objToFilter(this.standardPriceVersionLine.searchCondition) + ';EQ_standardPriceVersion.id=' + this.selectedStandardPriceVersion.id,
          sorts: '',
          page: this.standardPriceVersionLine.page,
          size: this.standardPriceVersionLine.size
        }).then(res => {
          this.standardPriceVersionLine.listLoading = false;
          if (res.data.successFlg) {
            this.standardPriceVersionLine.lineData = res.data.detailModelList;
            this.standardPriceVersionLine.total = res.data.totalCount;
          } else {
            this.$message.error(res.data.errorMsg);
          }
        });
      } else {
        this.standardPriceVersionLine.lineData = [];
        this.standardPriceVersionLine.total = 0;
      }
    },
    handleStandardPriceVersionCurrentChange (val) {
      this.selectedStandardPriceVersion = val;
      this.searchStandardPriceVersionLines();
    },
    submitStandardPriceVersionClick (id) {
      this.$confirm('您确认提交该版本?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        submitStandardPriceVersion(id)
          .then(res => {
            if (res.data.successFlg) {
              this.$message('提交成功！');
              this.$emit('submitStandardPriceVersionSuccess');
              this.loadVersions();
            } else {
              this.$message.error(res.data.errorMsg);
            }
          });
      }).catch(() => {});
    },
    addStandardPriceVersionClick () {
      let number = 'v1.0';
      if (this.versionData.length > 0) {
        let version = this.versionData[this.versionData.length - 1];
        number = 'v' + (parseFloat(version.number.substr(1)) + 0.1).toFixed(1);
      }
      this.$refs.standardPriceVersionAdd.show(number);
    },
    saveStandardPriceVersionLine (o) {
      createStandardPriceVersionLine(this.selectedStandardPriceVersion.id, o)
        .then(res => {
          if (res.data.successFlg) {
            this.$message('保存成功！');
            this.$refs.standardPriceVersionLineAdd.hide();
            this.searchStandardPriceVersionLines();
          } else {
            this.$message.error(res.data.errorMsg);
          }
        });
    },
    saveStandardPriceVersion (o) {
      o.status = 112014001;
      createStandardPriceVersion(this.standardPrice.id, o)
        .then(res => {
          if (res.data.successFlg) {
            this.$message('保存成功！');
            this.$refs.standardPriceVersionAdd.hide();
            this.loadVersions();
          } else {
            this.$message.error(res.data.errorMsg);
          }
        });
    },
    deleteStandardPriceVersionLineClick (id) {
      this.$confirm('您确认删除该线路?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteStandardPriceVersionLine(id)
          .then(res => {
            if (res.data.successFlg) {
              this.$message('删除成功！');
              this.searchStandardPriceVersionLines();
            } else {
              this.$message.error(res.data.errorMsg);
            }
          });
      }).catch(() => {});
    },
    deleteStandardPriceVersionClick (id) {
      this.$confirm('您确认删除该版本?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteStandardPriceVersion(id)
          .then(res => {
            if (res.data.successFlg) {
              this.$message('删除成功！');
              this.loadVersions();
            } else {
              this.$message.error(res.data.errorMsg);
            }
          });
      }).catch(() => {});
    },
    loadVersions () {
      this.listLoading = true;
      getAllStandardPriceVersions({
        filters: 'EQ_standardPrice.id=' + this.standardPrice.id,
        sorts: '+number'
      }).then(res => {
        this.listLoading = false;
        if (res.data.successFlg) {
          this.versionData = res.data.detailModelList;
        } else {
          this.$message.error(res.data.errorMsg);
        }
      });
    },
    show (o) {
      this.standardPrice = o;
      this.loadVersions();
      this.dialogVisible = true;
    }
  },
  mounted () {
    this.$store.commit('initDict');
  }
};
</script>
